<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/view/include/taglib.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">	
	<title>用户管理</title>

    <link href="${project}/ds/css/input.css" rel="stylesheet"/>
    <link href="${project}/ds/lib/ligerUI/skins/Gray2014/css/all.css" rel="stylesheet" />
    <link href="${project}/ds/lib/layui/css/layui.css" rel="stylesheet"/>
    <link href="${project}/ds/lib/font-awesome-4.7.0/css/font-awesome.css" rel="stylesheet" />
	<link href="${project}/ds/css/roomStyle.css" rel="stylesheet"/>

    <script src="${project}/ds/lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="${project}/ds/lib/ligerUI/js/core/base.js" type="text/javascript"></script>
    <script src="${project}/ds/lib/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
    <script src="${project}/ds/lib/jquery.form.js" type="text/javascript"></script>
    <script src="${project}/ds/lib/layui/layui.js" type="text/javascript"></script>
    <script src="${project}/ds/js/js/ws.js" type="text/javascript"></script>
    <script src="${project}/ds/js/XHD.js" type="text/javascript"></script>
    <script src="${project}/ds/js/room/curd.js" type="text/javascript"></script>
<script src="${project}/ds/js/layeralert.js" type="text/javascript"></script>
<script src="${project}/ds/js/room/command.js" type="text/javascript"></script>
    <script type="text/javascript">
        var manager;
        var form;
        var layer;
        var layerwindow;
        var user = getCookie("xhdcrm_uid");
        var DISABLEDCLASS = "layui-btn-disabled";
        var loadindex = 0;
        layui.use(['layer', 'form'], function () {
            var $ = layui.jquery;
            layer = layui.layer;
            form = layui.form();
            //显示菜单权限
            showAllMenus(form, $);

            //表单验证
            form.verify({
                user: function (value) {
                    var data = {
                        //Action: "Exist",
                        userName: value,
                        emp_id: $("#id").val()
                    }
                    var b = "";
                    if ($("#id").val() == "") {
                        b = "未选择管理员！";
                    }
                    else if ($.trim(value) === "") {
                        b = "用户名不能为空！";
                    }
                    
                    return b;
                }
            });
            //保存事件
            form.on('submit(save)',
                function (data) {
                    SaveHremployee(data);
//                    save(data);
                });
            //删除事件
            form.on('submit(del)',
                function () {
                    DeleteHremployee();
                });
        });
        $(function() {
            var intoParam = { op: "and", rules: [] };
            intoParam.rules.push({
                op: "notequal",
                field: "uid",
                value: "admin",
                type: "string"
            });
            manager = $("#maingrid4")
                .ligerGrid({
                    columns: [
                        { display: '账号', name: 'uid', width: 80 },
                        {
                            display: '姓名',
                            name: 'name',
                            width: 60
                        }
                    ],
                    rownumbers: true,
                    dataAction: 'server',
                    sortName: "id",
                    sortOrder: "desc",
                    pageSize: 50,
                    usePager: false,
                    cssClass: "black",
                    pageSizeOptions: [20, 30, 50, 100],
                    url: "/employee/getEmployeeGrid",
                    width: '100%',
                    height: '100%',
                    parms: [{ name: 'where', value: JSON.stringify(intoParam) }],
                    heightDiff: -1,
                    onSelectRow: function (data) {
                        showInfo(data);
                    },
                    onRClickToSelect: true,
                    alternatingRow: false
                });

            var width = window.innerWidth;
            $("#right").css("width", width - $("#maingrid4").width());
           // websocketConnect();
        });
        
        //发送新增消息
        function wsempadd(user, uid) {
            var json = wsorder.empadd(user, uid);
            websocket.send(JSON.stringify(json));
        }
        
        //发送删除消息
        function wsempdel(user, uid) {
            var json = wsorder.empdel(user, uid);
            websocket.send(JSON.stringify(json));
        }

        function showAllMenus(form, $) {
            $.ajax({
                url: "/employee/showAllMenus",
                type: "post",
                data: { Action: "getAllMenus" },
//              dataType: "json",
                success: function (data) {
                    //r = eval(r);
                    var r = data.Rows;
                    for (var z in r) {
                        var data = r[z].children;
                        var item = $("<div class='layui-form-item'></div>");
                        var label = '<label class="layui-form-label">'+r[z].name+'</label>';
                        item.append(label);
                        var div = $("<div  class='layui-input-block'></div>");
                        for (var i in data) {
                            if (data.hasOwnProperty(i)) {
                                var menu = data[i];

                                if (menu.name === "账号管理") {
                                    continue;
                                }
                                var checkbox = $("<input type='checkbox' class='menu' father="+r[z].id+" name='menus" +
                                    menu.id +
                                    "' value='" +
                                    menu.id +
                                    "' " +
                                    "title='" +
                                    menu.name +
                                    "" +
                                    "' />");
                                div.append(checkbox);
                            }
                        }
                        item.append(div);
                        $("#info").append(item);
                        form.render("checkbox");
                    }
                }
            });
        }

        //显示客户信息
        function showInfo(data) {
            $("input[name^=menus]").removeAttr("checked");
            $("#id").val(data.ID);
            $("#uid").val(data.uid);
            $("#name").val(data.name);
            var menus = data.menus;
            if (menus !== null && menus !== undefined && menus.length > 0) {
                var arr = menus.split(',');
                for (var i in arr) {
                    if (arr.hasOwnProperty(i)) {
                        var menu = arr[i];
                        $("input[name^=menus][value=" + menu + "]").attr("checked", "true");
                    }
                }
            }
            form.render("checkbox");
            $("#pwd").unbind("click").bind("click", function () {
                layer.prompt({
                    formType: 1,
                    title: '修改密码'
                }, function (value, index) {
                    changepwd(value);
                    layer.close(index);
                });
            });
            $("#pwd").removeClass(DISABLEDCLASS);
        }

        //重置客户信息
        function reset() {
            $("input[name^=menus]").removeAttr("checked");
            form.render("checkbox");
            $("#id").val("");
            $("#uid").val("");
            $("#name").val("");
            
            $("#pwd").addClass(DISABLEDCLASS).unbind("click");
        }

        function addsave() {
            var b = true;
            $.ajax({
                url: "/employee/checkEmployeeExist", type: "POST",
                data: { userName: $("#N_Username").val() },
                async: false,
                dataType: "json",
                success: function (data) {
                    if (data.result === "false") {
                        b = false;
                    }
                }
            });
            if (b) {
                $.ajax({
                    url: "/employee/saveEmployee",
                    type: "post",
                    data: { userName: $("#N_Username").val(), realName: $("#N_Name").val() },
                    dataType: "json",
                    success: function (data) {
                        var result = data.result;
                        if (result === "true") {
                           // wsempadd(user, $("#N_Username").val());
                            f_reload();
                            reset();
                            closewait();
                            layer.close();
                            layer.close(layerwindow);
                        }
                    }
                });
            }
            else {
                layer.msg("该用户已存在");
            }
        }
        //保存
        function SaveHremployee(data) {
        	var exist= false;
        	$.ajax({
                url: "/employee/checkEmployeeExist", type: "POST",
                data: data,
                async:false,
                success: function (data) {
                    if (data.result === "false") {
                    	exist=true;
                        b = "用户名已存在！";
                        console.log(b);
                    }
                }
            });
        	if(exist){
        		layer.msg("用户名已存在")
        		return;
        	}
            loadindex = layload("保存中");
            var menu = getMenus(data);
            data.field.menu = menu;
            //data.field.Action = "save";
            $.ajax({
                url: "/employee/saveEmployeeMenu",
                type: "post",
                data: data.field,
                dataType: "json",
                success: function(data) {
                    var result = data.result;
                    if (result === "true") {
                        //wsempadd(user, $("#uid").val());
                        f_reload();
                        reset();
                        closewait();
                    }
                }
            });
        }

        function getMenus(data) {
            var menu = "";
            var z =new Array();
            var Count = 0;
            var MenuList = $('.menu');
            for (var i = 0; i < MenuList.length; i++) {
                if (MenuList[i].checked) {
                    var bl = true;
                    for (var j = 0; j < Count; j++) {
                        if (z[j] === MenuList[i].attributes.father.value) {
                            bl = false;
                            break;
                        }
                    }
                    if (bl) {
                        z[Count] = MenuList[i].attributes.father.value;
                        Count++;
                    }
                }
            }
            for (var i = 0; i < Count; i++) {
                menu += z[i] + ",";
            }
            for (var i in data.field) {
                if (data.field.hasOwnProperty(i)) {
                    if (i.indexOf("menu") === 0) {
                        var value = data.field[i];
                        menu += value + ",";
                    }
                }
            }
            if (menu !== "") {
                menu = menu.substring(0, menu.length - 1);
            }
            return menu;
        }

        function openpwd() {
            $("#oldpwd").val("");
            $("#newpwd1").val("");
            $("#newpwd2").val("");
            $("#aux1").html("");
            $("#aux2").html("");
            $("#aux3").html("");
            layer.close();
            layer.open({
                type: 1,
                title: false,
                closeBtn: 0,
                area: '650px',
                skin: 'layui-layer-lan', //没有背景色
                shadeClose: true,
                content: $('#pwddiv')
            });
            pwdBindClick();
        }

        function openadd() {
            $("#N_Username").val("");
            $("#N_Name").val("");
            layer.close();
            layerwindow = layer.open({
                type: 1,
                title: false,
                closeBtn: 0,
                area: '300px',
                skin: 'layui-layer-lan', //没有背景色
                shadeClose: true,
                content: $('#adddiv')
            });
        }

        function pwdBindClick() {
            $("#oldpwd")
                .unbind("blur")
                .bind("blur",
                    function () {
                        var value = $(this).val();
                        if (value === "") {
                            $("#aux1").html("原密码不能为空！");
                            return;
                        }
                        var data = {
                            Action: "validatepwd",
                            T_oldpwd: value
                        }
                        $.ajax({
                            url: "../data/hr_employee.ashx", type: "POST",
                            data: data,
                            success: function (result) {
                                if (result === "false") {
                                    $("#aux1").html("原密码不正确！");
                                } else {
                                    $("#aux1").html("");
                                }
                            }
                        });
                    });
            $("#newpwd1")
                .unbind("blur")
                .bind("blur",
                    function () {
                        var value = $(this).val();
                        if (value === "") {
                            $("#aux2").html("新密码1不能为空！");
                        } else {
                            $("#aux2").html("");
                        }
                    });
            $("#newpwd2")
                .unbind("blur")
                .bind("blur",
                    function () {
                        var value1 = $("#newpwd1").val();
                        var value2 = $(this).val();
                        if (value2 === "") {
                            $("#aux3").html("新密码2不能为空！");
                        } else if (value1 !== value2) {
                            $("#aux3").html("两次输入密码不一致！");
                        } else {
                            $("#aux3").html("");
                        }
                    });
        }

        function changepwd(pwd) {
            var id = $("#id").val();
            var data = {
                //Action: "allchangepwd",
                empid: id,
                T_newpwd: pwd
            }
            var load = layer.load(1, { time: 1000 });
            $.ajax({
                url: "/employee/changePassword",
                type: "POST",
                data: data,
                success: function () {
                    layer.msg('修改成功！', {time:800});
                    layer.close(load);
                },
                error: function () {
                    layer.msg('操作失败！');
                    layer.close(load);
                }
            });
        }

        function wsempdelreply() {
            f_reload();
            reset();
            closewait();
        }

        function closewait() {
            layer.close(loadindex);
        }

        function DeleteHremployee() {
            var manager = $("#maingrid4").ligerGetGridManager();
            var row = manager.getSelectedRow();
            console.log(row)
            if (row) {
                layer.confirm(
                    "您确认要删除?",
                    {
                        btn: ["确定", "取消"]
                    },
                    function () {
                        loadindex = layload("删除中");
                        //wsempdel(user, row.uid);
                    	 $.ajax({
                             url: "/employee/delEmployee",
                             type: "post",
                             data: {id:row.ID},
                             dataType: "json",
                             success: function(data) {
                                 var result = data.result;
                                 if (result === "true") {
                                     f_reload();
                                     reset();
                                     closewait();
                                 }
                             }
                         });
                    }
                );
            } else {
                layer.msg("请选择置业顾问");
            }
        }

        function f_reload() {
            manager.loadData(true);
        };
    </script>
</head>
<body>

<form id="form1" onsubmit="return false">
    <div class="clearfix" style="height: 100%;">
        <div style="float: left; height: 100%; width: 250px;">
            <div id="maingrid4"></div>
        </div>
        <div id="right" style="height: 100%; float:left;" class="layui-form">
            <div style="width: 60%; margin:20px auto;" id="info">
                <input type="hidden" id="id" name="id" />
                <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                    <div class="layui-btn-group">
                      <div class="layui-btn" onclick="openadd()">增加</div>
                      <div lay-submit lay-filter="del" class="layui-btn">删除</div>
                    </div>
                </div>
                <div style="width:100%;">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-block">
                            <input type="text" id="uid" name="T_Uid" required lay-verify="user" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-block">
                            <input type="text" id="name" name="T_Name" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div style="width: 60%; margin:20px auto;">
                  <div class="layui-form-item">
                    <div class="layui-input-block">
                      <div id="sub" class="layui-btn" lay-submit lay-filter="save">保存</div>
                      <div id="pwd" class="layui-btn layui-btn-primary layui-btn-disabled">修改密码</div>
                    </div>
                  </div>
            </div>
        </div>
    </div>
    
    <div style="width: 500px; display:none; margin-left:100px;" id="pwddiv" class="layui-form">
        <div style="width:100%; margin:20px;">
            <div class="layui-form-item">
                <label class="layui-form-label">原密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="oldpwd" name="T_oldpwd" required lay-verify="pass" placeholder="请输入原密码" autocomplete="off" class="layui-input">
                </div>
                <div id="aux1" class="layui-form-mid layui-word-aux" style="color: #FF5722;">请输入原密码</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="newpwd1" name="T_newpwd" required lay-verify="new1" placeholder="请输入新密码" autocomplete="off" class="layui-input">
                </div>
                <div id="aux2" class="layui-form-mid layui-word-aux" style="color: #FF5722;"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="newpwd2" required lay-verify="new2" placeholder="请输入新密码" autocomplete="off" class="layui-input">
                </div>
                <div id="aux3" class="layui-form-mid layui-word-aux" style="color: #FF5722;"></div>
            </div>
        </div>
        <div style="width: 100%; margin:20px auto;">
                <div class="layui-form-item">
                <div class="layui-input-block">
                    <div id="Div1" class="layui-btn" lay-submit lay-filter="changepwd">确认修改</div>
                    <div id="Div2" class="layui-btn layui-btn-primary" lay-submit lay-filter="resetpwd">重置密码</div>
                </div>
                </div>
        </div>
    </div>

    <div style="width: 300px; display:none; " id="adddiv" class="layui-form">
        <div style="width:100px; margin-top:10px;margin-bottom:10px;padding-left:100px;font-size:24px">
            添加账号
        </div>
        <div style="width:100%; ">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:50px">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" id="N_Username" name="N_Username" required lay-verify="N_Username" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:50px">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" id="N_Name" name="N_Name" required lay-verify="N_Name" placeholder="请输入姓名" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div style="width: 100%; margin-left:104px auto;">
                <div class="layui-form-item">
                <div class="layui-input-block">
                    <div id="Div2" class="layui-btn" onclick="addsave()">确认添加</div>
                </div>
                </div>
        </div>
    </div>
</form>
</body>
</html>